/******************************************************************************
** 
** Find out the longest string. 
** 
** File name:	longest_string.c
** Compilation:	cc longest_string.c -o longest_string
** Execution:	./longest_string
** 
** Description:
** Reads the input, divides it into strings, finds out which string is the 
** longest and prints it.
** 
******************************************************************************/
#include <stdio.h>

#define MAX_LINE_LENGTH 1000

int get_line(char line[], int max_length);
void copy_line(char source[], char dest[]);

int main()
{

  int current_length, max_length;
  char current_line[MAX_LINE_LENGTH];
  char max_line[MAX_LINE_LENGTH];

    current_length = max_length = 0;
    while ((current_length = get_line(current_line, MAX_LINE_LENGTH)) != 0) {
        if (current_length > max_length) {
            max_length = current_length;
            copy_line(current_line, max_line);
        }
    }

    printf("\n\nLongest line is %d characters long:\n%s\n", max_length, max_line);
    return 0;
}


// get_line: Reads one line from the input and stores it in the array, returns line length.
int get_line(char line[], int max_length) {
    int c, i;

    for (i=0; i<max_length && (c=getchar()) != EOF && c != '\n'; ++i) {
        line[i] = c;
    }
    if (c == '\n') {
        line[i] = c;
        ++i;
    }
    line[i] = '\0';
    return i;
}


// copy_line: Copies one char array to another.
void copy_line(char source[], char dest[]) {
    int i;

    i = 0;
    while ((dest[i] = source[i]) != '\0') {
        ++i;
    }
}
